package org.adream.account.service;

import java.util.List;

import org.adream.account.dao.IntegralTypeDao;
import org.adream.account.entity.IntegralTypeEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class IntegralTypeService {
	
	@Autowired
	private IntegralTypeDao integralTypeDao;
	
	private final static Logger logger = LoggerFactory.getLogger(IntegralTypeService.class);
	
	/**
	 * 增加积分类型
	 * @param integralType
	 * @return
	 */
	public ResultModel<?> addIntegralType(IntegralTypeEntity integralType) {
		int result = integralTypeDao.addIntegralType(integralType);
		if(result == Constant.ZERO) {
			logger.warn("新增积分类型失败:(");
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误it118,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除积分类型
	 * @param integralTypeEntity
	 * @return
	 */
	public ResultModel<?> deleteIntegralType(IntegralTypeEntity integralTypeEntity) {
		IntegralTypeEntity integralType = integralTypeDao.queryIntegralTypeByItid(integralTypeEntity.getItid());
		if(integralType == null) {
			logger.warn("删除失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,请刷新重试", null);
		}
		integralType.setModifier(integralTypeEntity.getModifier());
		integralType.setDr(Constant.ZERO);
		int result = integralTypeDao.deleteIntegralType(integralType);
		if(result == Constant.ZERO) {
			logger.warn("删除积分类型失败:(");
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误it119,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更改积分类型
	 * @param integralTypeEntity
	 * @return
	 */
	public ResultModel<?> updateIntegralType(IntegralTypeEntity integralTypeEntity) {
		IntegralTypeEntity integralType = integralTypeDao.queryIntegralTypeByItid(integralTypeEntity.getItid());
		if(integralType == null) {
			logger.warn("更新失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "更新失败,请刷新重试", null);
		}
		integralType.setName(integralTypeEntity.getName());
		integralType.setModifier(integralTypeEntity.getModifier());
		int result = integralTypeDao.updateIntegralType(integralType);
		if(result == Constant.ZERO) {
			logger.warn("更新积分类型失败:(");
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误it120,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据dr查询
	 * @param dr
	 * @return
	 */
	public ResultModel<?> queryIntegralTypes(int dr) {
		List<IntegralTypeEntity> integralTypes = integralTypeDao.queryIntegralTypesByDr(dr);
		if(Utils.isEmptyCollection(integralTypes)) {
			logger.warn("查询失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<IntegralTypeEntity>>(ResultModel.SUCCESS, "查询成功", integralTypes);
	}
	
	/**
	 * 根据itid查询
	 * @param itid
	 * @return
	 */
	public ResultModel<?> queryIntegralTypeByItid(int itid) {
		IntegralTypeEntity integralType = integralTypeDao.queryIntegralTypeByItid(itid);
		if(integralType == null) {
			logger.warn("查询失败,数据不存在:(");
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<IntegralTypeEntity>(ResultModel.SUCCESS, "查询成功", integralType);
	}
}